package com.example.demo4.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo4.Service.EmpService;
import com.example.demo4.entity.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping("findPage")
    public String findPage(Integer pageIndex, Model model,String name){
        if (pageIndex==null)pageIndex=1;
       Page<Emp> page= empService.findAll(pageIndex,name);
//       model.addAttribute("page",page);
//       model.addAttribute("name",name);
       return "emp";
    }

    @GetMapping("toadd")
    public String toadd(){
        return "add";
    }


    @PostMapping("add")
    public String add(Emp emp){
        empService.save(emp);
        return "redirect:findPage";
    }

    @GetMapping("delete")
    public String delete(Integer id){
        empService.removeById(id);
        return "redirect:findPage";
    }


    @GetMapping("updateById")
    public String updateById(Integer id,Model model){
        Emp emp = empService.getById(id);
        model.addAttribute("emp",emp);
        return "update";
    }


    @PostMapping("update")
    public String update(Emp emp){
        empService.updateById(emp);
        return "redirect:findPage";
    }

}






















